package cn.itsource.service.impl;

import cn.itsource.doc.CourseDoc;
import cn.itsource.domain.Course;
import cn.itsource.dto.CourseAddDto;
import cn.itsource.exception.MyException;
import cn.itsource.feign.client.CourseFeignClient;
import cn.itsource.mapper.CourseDetailMapper;
import cn.itsource.mapper.CourseMapper;
import cn.itsource.mapper.CourseMarketMapper;
import cn.itsource.query.CourseQuery;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICourseService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jason
 * @since 2022-09-01
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Override
    public Page<CourseAddDto> selectMyPage(CourseQuery query) {
        Page<CourseAddDto> page = new Page<>(query.getPage(), query.getRows());
        List<CourseAddDto> courseAddDtoList = courseMapper.selectMyPage(page, query.getKeyword());
        return page.setRecords(courseAddDtoList);
    }
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CourseFeignClient courseFeignClient;
    //课程上线接口
    @Override
    public JSONResult onLineCourse(Long[] ids) {
        /**
         * 1、入参校验
         * 2、根据ID集合查询所有记录
         * 3、修改状态
         * 4、将课程信息保存到ES中
         */

        //查询课程详情
        List<CourseAddDto> courseAddDtoList = courseMapper.selectAllCourseInfo(ids);

        List<Course> updateCourseList = new ArrayList<>();
        Course course = null;
        for(int i=0; i<courseAddDtoList.size(); i++){
            CourseAddDto courseAddDto = courseAddDtoList.get(i);
            Integer status = courseAddDto.getCourse().getStatus();
            //课程状态，下线：0 ， 上线：1
            if(status == 0){
                //下线状态的课程才需要操作
                course = new Course();
                course.setId(ids[i]);
                course.setStatus(1);
                updateCourseList.add(course);
            }
        }
        if(CollectionUtils.isEmpty(updateCourseList)){
            throw new MyException("没有课程需要上线啦");
        }
        //批量修改课程状态
        super.updateBatchById(updateCourseList);

        //封装课程信息，准备调用ES接口做保存
        List<CourseDoc> docList = new ArrayList<>();
        CourseDoc doc = null;
        for(int i=0; i<courseAddDtoList.size(); i++){
            CourseAddDto courseAddDto = courseAddDtoList.get(i);
            doc = new CourseDoc();
            BeanUtils.copyProperties(courseAddDto.getCourse(), doc);
            BeanUtils.copyProperties(courseAddDto.getCourseMarket(), doc);
            BeanUtils.copyProperties(courseAddDto.getCourseDetail(), doc);
            docList.add(doc);
        }

        //课程上线
        JSONResult jsonResult = courseFeignClient.saveCourse(docList);
        if(!jsonResult.isSuccess()){
            throw new MyException(jsonResult.getMessage());
        }


        /**
         * 5、发布营销信息给客户
         *  5.1、查询数据库，获取到所有客户的手机号
         *  5.5、准备消息
         *  5.3、调用第三方接口发送短信
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", "1800000,1801111111, 18200000");
        jsonObject.put("msg", "哥，你关注的课程有上新，赶紧来抢购，前10名免费，<a href='http://www.baidu.com'>戳我</a>领取");

        rabbitTemplate.convertAndSend("hrm_exchange", "course.online.sms", jsonObject.toJSONString());

        return JSONResult.success();
    }

}
